package com.mhr.bike;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.Gradient;
import com.amap.api.maps.model.HeatmapTileProvider;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.TileOverlayOptions;

import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
    private MapView mapView = null;//地图生成
    int a=0;
    AMap maps;//地图监听
    private LinearLayout.LayoutParams mParams;
    private RelativeLayout mContainerLayout;
    private static final int[] ALT_HEATMAP_GRADIENT_COLORS = {
            Color.rgb( 0, 0, 255),
            Color.rgb( 0, 211, 248),
            Color.rgb(0, 255, 0),
            Color.rgb(185, 71, 0),
            Color.rgb(255, 0, 0)
    };

    public static final float[] ALT_HEATMAP_GRADIENT_START_POINTS = { 0.1f,
            0.2f, 0.25f, 0.4f, 1f };


    public static final Gradient ALT_HEATMAP_GRADIENT = new Gradient(
            ALT_HEATMAP_GRADIENT_COLORS, ALT_HEATMAP_GRADIENT_START_POINTS);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.MainActivity);
        mapView.onCreate(savedInstanceState);
// 调用 onCreate方法 对 MapView LayoutParams 设置
        init();

    }
    private void init()
    {
        maps=mapView.getMap();
        LatLng[] latlngs = new LatLng[200];
        double x = 38.870000;
        double y = 121.533530;

        for (int i = 0; i < 100; i++) {
            double x_ = 0;
            double y_ = 0;
            x_ = Math.random() * 0.005 - 0.0025;
            y_ = Math.random() * 0.005 - 0.0025;
            latlngs[i] = new LatLng(x + x_, y + y_);
        }
        x =38.86800;
        y =121.527530;

        for (int i = 100; i < 200; i++) {
            double x_ = 0;
            double y_ = 0;
            x_ = Math.random() * 0.005 - 0.0025;
            y_ = Math.random() * 0.005 - 0.0025;
            latlngs[i] = new LatLng(x + x_, y + y_);
        }
        // 构建热力图 HeatmapTileProvider
        HeatmapTileProvider.Builder builder = new HeatmapTileProvider.Builder();
        builder.data(Arrays.asList(latlngs)) // 设置热力图绘制的数据
                .gradient(ALT_HEATMAP_GRADIENT); // 设置热力图渐变，有默认值 DEFAULT_GRADIENT，可不设置该接口
// Gradient 的设置可见参考手册
// 构造热力图对象
        HeatmapTileProvider heatmapTileProvider = builder.build();
        // 初始化 TileOverlayOptions
        TileOverlayOptions tileOverlayOptions = new TileOverlayOptions();
        tileOverlayOptions.tileProvider(heatmapTileProvider); // 设置瓦片图层的提供者
// 向地图上添加 TileOverlayOptions 类对象
        //LatLng latLng = new LatLng(38.870662162482624,121.53519685711672);
        //final Marker marker = maps.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));
        maps.addTileOverlay(tileOverlayOptions);
        LatLng lating=new LatLng(38.86800, 121.530030);
        maps.moveCamera(CameraUpdateFactory.newLatLngZoom(lating,16));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();

        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }
}

